package com.anysoft.view.picker;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.base.BaseActivity;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;

public class NomarlPickerPopupWindow extends PopupWindow {

    private Button btn_cancel, btn_sure;
    private TextView title;
    private LoopView loopView;
    private View mMenuView;
    private ArrayList<String> list;

    private OnPickerChangeListener pickerChangeListener;

    private String title_str;

    private Context context;

    public void setTitle_str(String title_str) {
        this.title_str = title_str;

        title.setText(title_str);
    }

    public void setOnPickerChangeListener(OnPickerChangeListener pickerChangeListener) {
        this.pickerChangeListener = pickerChangeListener;
    }

    @SuppressLint("InflateParams")
    public NomarlPickerPopupWindow(Context context, OnClickListener itemsOnClick, ArrayList<String> list) {
        super(context);
        this.context = context;
        this.list = list;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.pop_identity_type_picker, null);
        btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
        btn_sure = (Button) mMenuView.findViewById(R.id.btn_sure);
        title = (TextView) mMenuView.findViewById(R.id.title);
        loopView = (LoopView) mMenuView.findViewById(R.id.loopView);
        // 设置按钮监听
        btn_cancel.setOnClickListener(itemsOnClick);
        btn_sure.setOnClickListener(itemsOnClick);

        // 设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopupAnimation);
        // TODO 实例化一个ColorDrawable颜色为半透明,即空余部分的颜色
//        ColorDrawable dw = new ColorDrawable(0x80000000);
		ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.transparent));
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            @SuppressLint("ClickableViewAccessibility")
            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

        //设置是否循环播放
        loopView.setNotLoop();
        //滚动监听
        loopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                if (pickerChangeListener != null) {
                    pickerChangeListener.onPickerChange(index);
                }
            }
        });
        //设置原始数据
        loopView.setItems(list);

        backgroundAlpha(0.5f);

    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        BaseActivity baseActivity = (BaseActivity) context;
        WindowManager.LayoutParams lp = baseActivity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        baseActivity.getWindow().setAttributes(lp);
    }

    /**
     * 选择器变化监听
     */
    public interface OnPickerChangeListener {

        void onPickerChange(int position);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        backgroundAlpha(1f);
    }
}